package com.tcxhb.job.admin.controller;

import com.tcxhb.job.admin.convert.JobMachineConvert;
import com.tcxhb.job.dao.dataobject.JobMachineDO;
import com.tcxhb.job.dao.dataobject.query.JobMachineQuery;
import com.tcxhb.job.admin.model.request.biz.JobMachineAddReq;
import com.tcxhb.job.admin.model.request.query.JobMachineQueryReq;
import com.tcxhb.job.admin.model.response.JobMachineVO;
import com.tcxhb.job.core.service.job.JobMachineService;
import com.tcxhb.job.common.constants.CommonErrorCode;
import com.tcxhb.job.common.model.MiscResult;
import com.tcxhb.job.common.model.PageResponse;
import com.tcxhb.job.common.request.CommonIdDeleteReq;
import com.tcxhb.job.common.request.CommonIdQueryReq;
import com.tcxhb.job.common.utils.ConvertUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * author:auto.generator
 * time: 2023-11-05
 */
@RestController
@RequestMapping("/job/machine")
public class JobMachineController extends BaseController {

    @Autowired
    private JobMachineConvert convert;
    @Autowired
    private JobMachineService jobMachineService;


    @PostMapping(value = "/page", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询")
    public MiscResult<PageResponse<JobMachineVO>> page(@RequestBody JobMachineQueryReq req) {
        fillAppName(req);
        JobMachineQuery query = convert.toQuery(req);
        PageResponse<JobMachineDO> response = jobMachineService.page(query);
        PageResponse<JobMachineVO> result = ConvertUtils.page(response, convert::do2VO);
        return MiscResult.suc(result);
    }

    @PostMapping(value = "/create", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建")
    public MiscResult<Boolean> create(@RequestBody JobMachineAddReq req) {
        JobMachineDO query = convert.req2DO(req);
        Long id = jobMachineService.create(query);
        return MiscResult.suc(true);
    }

    @PostMapping(value = "/queryById", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据ID查询")
    public MiscResult<JobMachineVO> queryById(@RequestBody CommonIdQueryReq req) {
        JobMachineDO relationDO = jobMachineService.queryById(req.getId());
        JobMachineVO vo = convert.do2VO(relationDO);
        return MiscResult.suc(vo);
    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新")
    public MiscResult<String> update(@RequestBody JobMachineAddReq req) {
        JobMachineDO query = convert.req2DO(req);
        Boolean result = jobMachineService.updateById(query);
        if (Boolean.TRUE.equals(result)) {
            return MiscResult.suc("操作成功");
        }
        return MiscResult.err(CommonErrorCode.BAD_REQUEST);
    }

    @PostMapping(value = "/deleteById", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除")
    public MiscResult<String> deleteById(@RequestBody CommonIdDeleteReq req) {
        Boolean result = jobMachineService.deleteById(req.getId());
        if (Boolean.TRUE.equals(result)) {
            return MiscResult.suc("操作成功");
        }
        return MiscResult.err(CommonErrorCode.BAD_REQUEST);
    }
}
